package com.shuoyue.ycgk.ui.questionbank.base;

import android.util.Log;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.ui.questionbank.practice.freedom.FragmentSpace;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

public class BaseMateriAdapter<T extends BaseMateriFragment, K extends BaseSimpleFragment>
        extends FragmentStatePagerAdapter {
    List<QuestionParent> mData;
    private BaseMvpFragment lastFragment;
    private BaseMvpFragment mCurrentFragment;
    private BaseMvpFragment nextFragment;
    int totalQuestion;
    Class<K> kClass;
    Class<T> tClass;

    public BaseMateriAdapter(@NonNull FragmentManager fm, List<QuestionParent> mData, int totalQuestion, Class tClass,
                             Class kClass) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        if (mData != null) {
            this.mData = mData;
        } else {
            this.mData = new ArrayList<>();
        }
        this.totalQuestion = totalQuestion;
        this.tClass = tClass;
        this.kClass = kClass;

    }

    @NonNull
    @Override
    public BaseMvpFragment getItem(int position) {
        Log.e("position", position + "");
        QuestionParent questionParent = mData.get(position);
        if (questionParent.isSpcePage()) {
            return FragmentSpace.getInstance(questionParent);
        } else if (questionParent.getIsMaterial() == 1) {
            try {
                T baseMateriFragment = tClass.newInstance();
                baseMateriFragment.init(questionParent, totalQuestion);
                return baseMateriFragment;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        } else {
            if (questionParent.getQuestionSimpleDTOS() != null && questionParent.getQuestionSimpleDTOS().size() > 0) {
                QuestionSimple questionSimple = questionParent.getQuestionSimpleDTOS().get(0);
                if (questionSimple.getQuestionType() != 10) {
                    try {
                        K baseSimpleFragment = kClass.newInstance();
                        baseSimpleFragment.init(questionSimple, totalQuestion);
                        return baseSimpleFragment;
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        lastFragment = mCurrentFragment;

        mCurrentFragment = (BaseMvpFragment) object;
        super.setPrimaryItem(container, position, object);
    }


    public BaseMvpFragment getCurrentFragment() {
        return mCurrentFragment;
    }

    public List<BaseMvpFragment> getAllFragment() {
        List<BaseMvpFragment> fragments = new ArrayList<>();
        for (int i = 0; i < mData.size(); i++) {
            Fragment fragment = getItem(i);
            if (fragment != null) {
                fragments.add((BaseMvpFragment) fragment);
            }
        }
        return fragments;
    }


}
